package xin.yangshuai.transactional01.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import xin.yangshuai.transactional01.dao.TestUserMapper;
import xin.yangshuai.transactional01.model.TestUser;
import xin.yangshuai.transactional01.service.PropagationServiceB;

@Service
public class PropagationServiceBImpl implements PropagationServiceB {

    @Autowired
    private TestUserMapper testUserMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void required() {
        update3();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void required1() {
        update3();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public void supports() {
        update3();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public void supports1() {
        update3();
    }

    @Override
    @Transactional(propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
    public void mandatory() {
        update3();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
    public void mandatory1() {
        update3();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void requiresNew() {
        update3();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void requiresNew1() {
        update3();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, rollbackFor = Exception.class)
    public void notSupported() {
        update3();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, rollbackFor = Exception.class)
    public void notSupported1() {
        update3();
    }

    @Override
    @Transactional(propagation = Propagation.NEVER, rollbackFor = Exception.class)
    public void never() {
        update3();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.NEVER, rollbackFor = Exception.class)
    public void never1() {
        update3();
    }

    @Override
    @Transactional(propagation = Propagation.NESTED, rollbackFor = Exception.class)
    public void nested() {
        update3();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.NESTED, rollbackFor = Exception.class)
    public void nested1() {
        update3();
    }

    @Override
    public void no() {
        update3();
        int i = 1 / 0;
    }

    @Override
    public void no1() {
        update3();
    }

    private void update3() {
        TestUser testUser3 = new TestUser();
        testUser3.setId(3);
        testUser3.setName("cc");
        testUser3.setAge(35);
        testUserMapper.updateByPrimaryKey(testUser3);
        System.out.println("update testUser3");
    }

    private void update4() {
        TestUser testUser4 = new TestUser();
        testUser4.setId(4);
        testUser4.setName("dd");
        testUser4.setAge(45);
        testUserMapper.updateByPrimaryKey(testUser4);
        System.out.println("update testUser4");
    }
}
